package net.youball.dao.impl;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import net.youball.dao.IBaseDao;
import net.youball.utils.page.Args;
import net.youball.utils.page.PageBean;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T>{

	@SuppressWarnings("unchecked")
	private Class clas;
	
	public BaseDaoImpl(){
		
	}
	@SuppressWarnings("unchecked")
	public BaseDaoImpl(Class clas){
		this.clas=clas;
		getClass().getGenericSuperclass();
	}
	
	public Serializable add(T t){
		//this.getSession().beginTransaction();
		Serializable id=this.getSession().save(t);
		//this.getSession().beginTransaction().commit();
		return id;
	}

	@SuppressWarnings("unchecked")
	public T selectById(Serializable id) {
		// TODO Auto-generated method stub
		
		T t=(T) this.getSession().get(this.clas, id);
		
		return t;
	}
	@SuppressWarnings("unchecked")
	public PageBean queryWhere(PageBean pageBean) {
		// TODO Auto-generated method stub
		Criteria c=this.getSession().createCriteria(this.clas);
		if(pageBean!=null&&pageBean.getArgs()!=null&&pageBean.getArgs().size()!=0){
			Map<String,Args> map=pageBean.getArgs();
			Iterator<Entry<String,Args>> keys=map.entrySet().iterator();
			while(keys.hasNext()){
				Entry<String,Args> key=keys.next();
				SimpleExpression simp=null;
				if(key.getValue().comp.equals("asc")){
					c.addOrder(Order.asc(key.getKey()));
					continue;
				}else if(key.getValue().comp.equals("desc")){
					c.addOrder(Order.desc(key.getKey()));
					continue;
				}else if(key.getValue().comp.equals("between")){
					c.add(Restrictions.between(key.getKey(), key.getValue().getValue(),key.getValue().getValueThen()));
					continue;
				}else if(key.getValue().comp.equals("=")){
					simp=(Restrictions.eq(key.getKey(), key.getValue().getValue()));
				}else if(key.getValue().comp.equals("like"))
					simp=(Restrictions.like(key.getKey(),key.getValue().getValue().toString(),MatchMode.ANYWHERE));
				else if(key.getValue().comp.equals("likeleft"))
					simp=(Restrictions.like(key.getKey(),key.getValue().getValue().toString(),MatchMode.END));
				else if(key.getValue().comp.equals("likeright"))
					simp=(Restrictions.like(key.getKey(),key.getValue().getValue().toString(),MatchMode.START));
				else if(key.getValue().comp.equals("gt"))
					simp=(Restrictions.gt(key.getKey(), key.getValue().getValue()));
				else if(key.getValue().comp.equals("ge"))
					simp=(Restrictions.ge(key.getKey(), key.getValue().getValue()));
				else if(key.getValue().comp.equals("lt"))
					simp=(Restrictions.lt(key.getKey(), key.getValue().getValue()));
				else if(key.getValue().comp.equals("le"))
					simp=(Restrictions.le(key.getKey(), key.getValue().getValue()));
				
				c.add(simp);	
			}
			int size=((Number)c.setProjection(Projections.rowCount()).uniqueResult()).intValue();
			pageBean.setMax(size);
			c.setFirstResult(pageBean.getBegin());
			c.setMaxResults(pageBean.getCount());
			pageBean.setData(c.list());
		}else{
			int size=((Number)c.setProjection(Projections.rowCount()).uniqueResult()).intValue();
			pageBean.setMax(size);
			c.setFirstResult(pageBean.getBegin());
			c.setMaxResults(pageBean.getCount());
			pageBean.setData(c.list());
		}
		
		return pageBean;
	}
	
	public PageBean paging(PageBean pageBean){
		return this.queryWhere(pageBean);
	}

}
